import React, { FC, useEffect } from 'react';
import { Modal, Form, InputNumber, Input, Select } from 'antd';
import { PersonAccountItem } from '@/services/person/account';

interface CreditProps {
  visible: boolean;
  personAccountItem: Partial<PersonAccountItem> | undefined;
  onSubmit: (values: PersonAccountItem) => void;
  onCancel: () => void;
}

const formLayout = {
  labelCol: { span: 7 },
  wrapperCol: { span: 13 },
};

const CreditForm: FC<CreditProps> = (props) => {
  const [form] = Form.useForm();
  const { visible, personAccountItem, onCancel, onSubmit } = props;

  useEffect(() => {
    if (form && !visible) {
      form.resetFields();
    }
  }, []);

  useEffect(() => {
    if (personAccountItem) {
      form.setFieldsValue({
        ...personAccountItem,
        accountType: 'credit',
      });
    } else {
      form.setFieldsValue({
        accountType: 'credit',
      });
    }
  }, [props.personAccountItem]);

  const handleSubmit = () => {
    if (!form) return;
    form.submit();
  };

  const handleFinish = (values: { [key: string]: any }) => {
    if (onSubmit) {
      onSubmit({ ...values, accountType: 'credit' } as PersonAccountItem);
    }
  };

  const modalFooter = { okText: '保存', onOk: handleSubmit, onCancel };

  const renderContent = () => {
    return (
      <>
        <Form.Item
          label="账户名称"
          name="accountName"
          rules={[{ required: true, message: '请输入账户名称' }]}
        >
          <Input placeholder="请输入账户名称" />
        </Form.Item>
        <Form.Item
          label="信用额度"
          name="inAmount"
          rules={[{ required: true, message: '请输入信用额度' }]}
        >
          <InputNumber
            style={{ width: '100%' }}
            step={1000}
            formatter={(value) => `￥ ${value}`.replace(/\B(?=(\d{3})+(?!\d))/g, ',')}
            placeholder="请输入信用额度"
          />
        </Form.Item>
        <Form.Item label="可用金额" 
          name="outAmount"
          rules={[{ required: true, message: '请输入可用金额"' }]}>
          <InputNumber
            style={{ width: '100%' }}
            step={1000}
            formatter={(value) => `￥ ${value}`.replace(/\B(?=(\d{3})+(?!\d))/g, ',')}
            placeholder="请输入可用金额"
          />
        </Form.Item>
        <Form.Item
          label="账单日"
          name="creditDay"
          rules={[{ required: true, message: '请选择账单日' }]}
        >
          <Select placeholder="请选择账单日">
            <Select.Option value="01">01日</Select.Option>
            <Select.Option value="02">02日</Select.Option>
            <Select.Option value="03">03日</Select.Option>
            <Select.Option value="04">04日</Select.Option>
            <Select.Option value="05">05日</Select.Option>
            <Select.Option value="06">06日</Select.Option>
            <Select.Option value="07">07日</Select.Option>
            <Select.Option value="08">08日</Select.Option>
            <Select.Option value="09">09日</Select.Option>
            <Select.Option value="10">10日</Select.Option>
            <Select.Option value="11">11日</Select.Option>
            <Select.Option value="12">12日</Select.Option>
            <Select.Option value="13">13日</Select.Option>
            <Select.Option value="14">14日</Select.Option>
            <Select.Option value="15">15日</Select.Option>
            <Select.Option value="16">16日</Select.Option>
            <Select.Option value="17">17日</Select.Option>
            <Select.Option value="18">18日</Select.Option>
            <Select.Option value="19">19日</Select.Option>
            <Select.Option value="20">20日</Select.Option>
            <Select.Option value="21">21日</Select.Option>
            <Select.Option value="22">22日</Select.Option>
            <Select.Option value="23">23日</Select.Option>
            <Select.Option value="24">24日</Select.Option>
            <Select.Option value="25">25日</Select.Option>
            <Select.Option value="26">26日</Select.Option>
            <Select.Option value="27">27日</Select.Option>
            <Select.Option value="28">28日</Select.Option>
          </Select>
        </Form.Item>
        <Form.Item
          label="还款日"
          name="dueDay"
          rules={[{ required: true, message: '请选择还款日' }]}
        >
          <Select placeholder="请选择还款日">
            <Select.Option value="01">01日</Select.Option>
            <Select.Option value="02">02日</Select.Option>
            <Select.Option value="03">03日</Select.Option>
            <Select.Option value="04">04日</Select.Option>
            <Select.Option value="05">05日</Select.Option>
            <Select.Option value="06">06日</Select.Option>
            <Select.Option value="07">07日</Select.Option>
            <Select.Option value="08">08日</Select.Option>
            <Select.Option value="09">09日</Select.Option>
            <Select.Option value="10">10日</Select.Option>
            <Select.Option value="11">11日</Select.Option>
            <Select.Option value="12">12日</Select.Option>
            <Select.Option value="13">13日</Select.Option>
            <Select.Option value="14">14日</Select.Option>
            <Select.Option value="15">15日</Select.Option>
            <Select.Option value="16">16日</Select.Option>
            <Select.Option value="17">17日</Select.Option>
            <Select.Option value="18">18日</Select.Option>
            <Select.Option value="19">19日</Select.Option>
            <Select.Option value="20">20日</Select.Option>
            <Select.Option value="21">21日</Select.Option>
            <Select.Option value="22">22日</Select.Option>
            <Select.Option value="23">23日</Select.Option>
            <Select.Option value="24">24日</Select.Option>
            <Select.Option value="25">25日</Select.Option>
            <Select.Option value="26">26日</Select.Option>
            <Select.Option value="27">27日</Select.Option>
            <Select.Option value="28">28日</Select.Option>
          </Select>
        </Form.Item>
        <Form.Item 
          label="宽限天数"
          name="graceDays"
          rules={[{ required: true, message: '请输入宽限天数' }]}
        >
          <InputNumber
            style={{ width: '100%' }}
            max={10}
            step={1}
          />
        </Form.Item>
        <Form.Item label="备注" name="remark">
          <Input.TextArea rows={4} placeholder="请输入备注" />
        </Form.Item>
      </>
    );
  };

  return (
    <Modal
      title={`信用账户 添加`}
      width={640}
      bodyStyle={{ padding: '28px 0 0' }}
      destroyOnClose
      open={visible}
      {...modalFooter}
    >
      <Form {...formLayout} form={form} onFinish={handleFinish}>
        {renderContent()}
      </Form>
    </Modal>
  );
};

export default CreditForm;